angular.module('start.user.money.addmoney.ctrl', [])

  .controller('addmoneyCtrl', function($scope, $rootScope, $$UserMoneyService, $location, $ionicLoading) {
    console.log("addmoneyCtrl");

    $scope.moneyData = {
      money: 0,
      profit: 0,
      bills: []
    };

    $scope.formData = {
      paytype: 1, // type 1 微信 2支付宝
      money: 0
    };

    $scope.payparms = {};
    $scope.payorderId = "";

    $scope.premoney = [
      { money: 50, select: false },
      { money: 100, select: false },
      { money: 200, select: false },
      { money: 300, select: false },
      { money: 500, select: false },
      { money: 1000, select: false },
      { money: 1500, select: false },
      { money: 2000, select: false },
    ];
    $scope.selectpremoney = function(index) {
      for (var i in $scope.premoney) {
        $scope.premoney[i].select = false;
      }
      $scope.premoney[index].select = true;
      $scope.formData.money = $scope.premoney[index].money;
    };

    // 查看余额信息
    $scope.getMoneymsg = function() {
      $$UserMoneyService.getusermoneymsg()
        .then(function(res) {
          $scope.moneyData.money = res.data.money;
        });
    };
    $scope.getMoneymsg();

    // 充值提交
    $scope.addmoney_sub = function() {
      if (!checkPrice($scope.formData.money) || $scope.formData.money <= 0) {
        $ionicLoading.show({
          template: "请正确填写价格",
          noBackdrop: true,
          duration: 1500
        });
        return false;
      }
      $$UserMoneyService.userAddMoney($scope.formData)
        .then(function(res) {
          $scope.payparms = res.data.mingwen;
          $scope.payorderId = res.data.orderId;
          $ionicLoading.show({
            template: res.msg,
            noBackdrop: true,
            duration: 1500
          });
          $scope.usepay();
        });
    };




    // 调用支付
    $scope.usepay = function() {
      // 支付宝支付方式
      if ($scope.formData.paytype == 2) {
        $scope.payparms = parseStrObjByRegExp($scope.payparms);
        console.log($scope.payparms);
        // alert("alipay");
        // 如果是cordova App应用
        if ($rootScope.config.isWebView) {
          var paramsWechatPay = {
            tradeNo: $scope.payorderId,
            subject: "伴你同游支付", //商品名称
            body: "伴你同游支付", //商品描述
            price: $scope.formData.money,
            notifyUrl: $scope.payparms.notify_url
          };
          // alert(JSON.stringify(paramsWechatPay));
          // 发起调用原生支付宝
          window.alipay.pay(paramsWechatPay,
            function(successResults) {
              // alert(JSON.stringify(successResults));
              if (successResults.resultStatus == 9000) { // 订单支付成功
                $ionicLoading.show({
                  template: "支付宝支付成功",
                  noBackdrop: true,
                  duration: 1500
                });
                $location.path("tab/user");
              }
            },
            function(errorResults) {
              // alert(JSON.stringify(errorResults));
              $ionicLoading.show({
                template: "支付宝支付失败",
                noBackdrop: true,
                duration: 1500
              });
            });
          $ionicLoading.show({
            template: "启用支付宝中",
            noBackdrop: true
          });
        } else {
          $ionicLoading.show({
            template: "请使用app支付",
            noBackdrop: true,
            duration: 1500
          });
        }
      }
      // 微信支付方式
      else if ($scope.formData.paytype == 1) {
        $scope.payparms = JSON.parse($scope.payparms);
        // {
        //   appid: "wxd130206eed2bb574",
        //   code: 1,
        //   message: null,
        //   noncestr: "D17E6BCBCEF8DE3F7A00195CFA5706F1",
        //   packageValue: "Sign=WXPay",
        //   partnerid: "1491193162",
        //   prepay_id: "wx20180116163229356070f3ff0108795524",
        //   sign: "1B1A9D5557BFB3795F87D41EF1706057",
        //   timestamp: "1516091549",
        // }
        console.log($scope.payparms);
        // 如果在微信浏览器中 调用微信js jdk支付
        if (!$rootScope.config.isWebView) {
          // 不是cordova App应用
          $ionicLoading.show({
            template: "请使用app支付",
            noBackdrop: true,
            duration: 1500
          });
        }
        // 如果是cordova App应用 调用微信原生支付
        else if ($rootScope.config.isWebView) {
          $ionicLoading.show({
            template: "启用微信支付中",
            noBackdrop: true
          });
          var paramsWechatPay = {
            partnerid: $scope.payparms.partnerid,
            prepayid: $scope.payparms.prepay_id,
            noncestr: $scope.payparms.noncestr,
            timestamp: $scope.payparms.timestamp,
            sign: $scope.payparms.sign,
          };
          // alert(JSON.stringify(paramsWechatPay));
          // 调用微信原生支付
          Wechat.sendPaymentRequest(paramsWechatPay,
            function(data) {
              // 支付成功后的回调函数
              $ionicLoading.show({
                template: "微信支付成功",
                noBackdrop: true,
                duration: 1500
              });
              $location.path("tab/user");
            },
            function(errorResults) { // 支付失败
              // alert(JSON.stringify(errorResults));
              $ionicLoading.show({
                template: "微信支付失败",
                noBackdrop: true,
                duration: 1500
              });
            });

        } //调用微信原生支付
        else {
          $ionicLoading.show({
            template: "请使用app支付",
            noBackdrop: true,
            duration: 1500
          });
        }
      } // 微信支付方式
    };

  });
